package com.egame.manage.biz.web.controller.pu;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import cn.egame.common.servlet.WebUtils;
import cn.egame.common.util.Utils;
import cn.egame.interfaces.EnumType.AppParameterType;
import cn.egame.interfaces.EnumType.GameStatus;
import cn.egame.interfaces.EnumType.GameTagType;
import cn.egame.interfaces.EnumType.NetworkType;
import cn.egame.interfaces.gc.GameInfo;
import cn.egame.interfaces.gc.GameTagLinkInfo;
import cn.egame.interfaces.pu.AppParameter;
import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.server.gc.GameServiceImpl;
import com.egame.manage.biz.server.gc.GameTagServiceImpl;
import com.egame.manage.biz.server.pu.ParameterServiceImpl;
import com.egame.manage.biz.web.controller.BaseController;
import com.egame.manage.biz.web.controller.gc.TerminalController;
import com.egame.manage.biz.utils.ConstVar;
import com.lenovo.manager.business.domain.UserBean;

@Controller
@RequestMapping("/tv/parameter")
public class NewParameterController  extends BaseController{
	public NewParameterController(){		
	}
	public NewParameterController(HttpServletRequest req,
			HttpServletResponse resp) {
		super(req, resp);
	}

	@RequestMapping(value = "/list/recommend", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView listRecommendPosition(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int parentId = WebUtils.getInt(req,"parentId",0);
		int position = WebUtils.getInt(req,"position",0);
		String gameName = WebUtils.getString(req, "gname","");
		String recommendname = WebUtils.getString(req, "recommendname","");
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, position);	
		ModelAndView mv = new ModelAndView("/new/op/recommendList");
		mv.addObject("position", position);
		mv.addObject("gameName", gameName);
		mv.addObject("recommendName", recommendname);
		
		List<Integer> gids = GameTagServiceImpl.getInstance().listGidByNotTagId(tagid,0,gameName);
		gids = GameServiceImpl.getInstance().listGameIdsByGameStatus(gids, GameStatus.business);
		List<GameInfo> gameList = GameServiceImpl.getInstance().listGameInfoByIds(appId, loginUserId, gids);		
				
		List<GameInfo> recommendGameList = GameTagServiceImpl.getInstance().listGameInfoByTagId(appId, loginUserId, tagid,recommendname);	
		mv.addObject("recommendGameList",recommendGameList);
		
		if(position==4 || position==5){
			int productTagId = 9;
			if(position==5){
				productTagId = 18;
			}
			productTagId = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, productTagId);
			gameList = GameTagServiceImpl.getInstance().listGameInfoByTagId(appId, loginUserId, productTagId, gameName);
			
		}else if(position==6){
			GameInfo tmp = new GameInfo();
			tmp.setFeeCode(3);
			gameList = GameServiceImpl.getInstance().listGameInfo(appId,loginUserId,tmp);
			for(int i=0;i<gameList.size();i++){
				if(gameList.get(i).getFeeCode()!=3){
					gameList.remove(i);
				}
			}	
		}else if(position==7){
			GameInfo tmp = new GameInfo();
			tmp.setNetworkType(NetworkType.onlineGame);
			gameList = GameServiceImpl.getInstance().listGameInfo(appId,loginUserId,tmp);
			for(int i=0;i<gameList.size();i++){
				if(gameList.get(i).getNetworkType() != null && gameList.get(i).getNetworkType().value() == 11){
					gameList.remove(i);
				}
			}				
		}
				
		for(int i=0;i<gameList.size();i++){
			for(int j=0;j<recommendGameList.size();j++){
				if(i<gameList.size() && gameList.get(i).getGId() ==recommendGameList.get(j).getGId()){
					gameList.remove(i);
				}
			}
		}
		
		if(!Utils.stringIsNullOrEmpty(gameName)){
			for(int i=0;i<gameList.size();i++){
				if(gameName.equals(gameList.get(i).getGameName())){
					gameList.remove(i);
				}
			}
		}	
		
		mv.addObject("gameList", gameList);
		
		String tagName = cn.egame.interfaces.ConstVar.HOT_LABEL;
		if(position == 3){	//新品
			tagName = cn.egame.interfaces.ConstVar.NEW_LABEL;
		}
		
		int tagId = ManageClient.getInstance().getGameTagIdByGameTagName(appId, loginUserId, GameTagType.in_tag, tagName);
		
		Map<Integer, GameTagLinkInfo> map = new HashMap<Integer, GameTagLinkInfo>();
		GameTagLinkInfo linkBean = null;
		for(GameInfo bean:recommendGameList){
			linkBean = ManageClient.getInstance().getGameTagLinkInfoByGId(appId, loginUserId, bean.getGId(), tagId);
			if(linkBean != null){
				map.put(bean.getGId(), linkBean);
			}
		}
		mv.addObject("map",map);
		
		List<Integer> ids = new ArrayList<Integer>();
		if(parentId<=0){
			ids = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, 1);
			ids.addAll(ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.RECOMMEND_TYPE));
		}		
		
		List<AppParameter> list = ManageClient.getInstance().listAppParameterByIds(position, tagid, ids);
		mv.addObject("positionList", list);
		
		return mv;		
	}	

	@RequestMapping(value = "/add", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView addParameter(AppParameter bean,HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		if(!ParameterServiceImpl.getInstance().hasExist(bean.getParentId(), bean.getName())){
			bean.setEnable(true);
			bean.setType(AppParameterType.lookup(WebUtils.getInt(req, "typeNum", 0)));
			bean.setSort(ParameterServiceImpl.getInstance().getMaxSortNo(bean.getParentId())+1);
			bean.setRemark("");			
			ManageClient.getInstance().setAppParameter(appId, loginUserId, bean);	
		}			
		return new TerminalController().showNewPage(req,resp);		
	}	
}
